#include "threadPool.h"
void *threadFunc(void *arg) {
    threadPool_t *threadPool = (threadPool_t *)arg;
    while (1) {
        // 等唤醒（等待netfd）
        pthread_mutex_lock(&threadPool->taskQueue.mutex);
        printf("tid = %ld, worker thread is created successfully!\n",
               pthread_self());
        while (threadPool->taskQueue.queueSize <= 0) {
            pthread_cond_wait(&threadPool->taskQueue.cond,
                              &threadPool->taskQueue.mutex);
        }
        printf("tid = %ld, worker thread got a task!\n", pthread_self());
        int netfd = threadPool->taskQueue.pFront->netfd;
        deQueue(&threadPool->taskQueue);

        MYSQL *mysql = mysqlInit(); // 初始化连接，该子线程和数据库建立连接
        printf("mysql already!\n");
        printf("001\n");
        // 解锁
        pthread_mutex_unlock(&threadPool->taskQueue.mutex);

        // 初始化相关变量，接收客户端发来的信息
        // 1.先创建、初始化 用来接收客户端发来的 注册/登录
        // 的信息，后面关于cd命令的，再新建变量接收
        cmdInfo_t login001;
        userInfo_t user;
        memset(&login001, 0, sizeof(login001));
        memset(&user, 0, sizeof(user));
        // 先写个接收中转函数，用于接收SIGN_UP或者SIGN_IN，根据不同的接收，进入不同的处理函数
        // 这个函数工作逻辑，先接收SIGN_UP/SIGN_IN，进入实际干事的函数，再分别接收另外的数据，进行操作
        int ret = recvLogIn(netfd, &login001, &user, mysql);
        if(ret == -1){
            break;
        }
        printf("recvLogIn ret = %d\n", ret);
        // 2.接收命令、执行命令
        cmdInfo_t cmdinfo;
        memset(&cmdinfo, 0, sizeof(cmdInfo_t));
        while (1) {

            int length;
            // 接收客户端发来的命令
            ssize_t ret = recv(netfd, &length, sizeof(int), MSG_WAITALL);
            if(ret == 0)
            break;
            printf("length = %d\n", length);
            recv(netfd, &cmdinfo, length, MSG_WAITALL);
            printf("cmdinfo.argLength = %d\n", cmdinfo.argLength);
            printf("cmdinfo.cmd = %d\ncmdinfo.userName = %s\ncmdinfo.arg = "
                   "%s\ncmdinfo.argLength = %d\ncmdinfo.fileName = %s\n",
                   cmdinfo.cmd, cmdinfo.userName, cmdinfo.arg,
                   cmdinfo.argLength, cmdinfo.fileName);
            // 根据用户名找其在user表中的用户ID
            char sql[4096];
            sprintf(sql, "SELECT id FROM `user` WHERE userName = '%s';",
                    cmdinfo.userName);
            printf("sql = %s\n", sql);
            MYSQL_RES *result = mysqlSearch(sql, mysql);
            if (result == NULL) {
                printf("mysql find no data\n");
            }
            MYSQL_ROW row = mysql_fetch_row(result);
            if (row == NULL) {
                printf("row find no data\n");
            }
            cmdinfo.user_id = atoi(row[0]);
            printf("user_id = %d----------------------------\n",
                   cmdinfo.user_id);
            // 此时已成功接收命令结构体！
            // 根据命令结构体中的cmd，转入对应的接口
            findCmdInterfaceServer(netfd, &cmdinfo, mysql);
        }
        // 关闭netfd
        close(netfd);
    }
    pthread_exit(NULL);
}
int makeWorker(threadPool_t *threadPool) {
    for (size_t i = 0; i < threadPool->workerNum; i++) {
        pthread_create(&threadPool->tidArr[i], NULL, threadFunc,
                       threadPool); // 看看要不要传参数
    }
}
int findCmdInterfaceServer(int netfd, cmdInfo_t *pcmdinfo, MYSQL *mysql) {
    printf("--------------------------from findCmdInterfaceServer--------------------------");
    switch (pcmdinfo->cmd) {
    case LS:
        printf("from findCmdInterfaceServer: user_id = %d\n",pcmdinfo->user_id);
        LS_Server(netfd, pcmdinfo, mysql);
        break;
    case CD:
        CD_Server(netfd, pcmdinfo, mysql);
        break;
    case PUTS:
        PUTS_Server(netfd, pcmdinfo, mysql);
        break;
    case GETS:
        GETS_Server(netfd, pcmdinfo, mysql);
        break;
    case REMOVE:
        REMOVE_Server(netfd, pcmdinfo, mysql);
        break;
    case PWD:
        PWD_Server(netfd, pcmdinfo, mysql);
        break;
    case MKDIR:
        MKDIR_Server(netfd, pcmdinfo, mysql);
        break;

    default:
        return -1;
        break;
    }
}
